﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Aleator.Core
{
    /// <summary>
    /// Concrete factory for progression creation
    /// </summary>
    public class ConcreteProgressionFactory : ProgressionFactory
    {
        /// <summary>
        /// Create a progression
        /// </summary>
        /// <param name="composition">Composition this progression exists within</param>
        /// <param name="comp">XML node for the parent composition</param>
        /// <param name="prog">XML node fot the progression</param>
        /// <returns></returns>
        public override Progression GetProgression(Composition composition, XmlNode comp, XmlNode prog)
        {
            ConcreteProgression cp = new ConcreteProgression();

            cp.Composition = composition;
            cp.CompositionID = composition.CompositionID;
            cp.ProgressionID = Convert.ToByte(comp.InnerText);
            cp.SequenceID = Int32.Parse(comp.Attributes.GetNamedItem("SequenceID").InnerText);

            cp.Key = composition.KeySignature;

            if (prog.Attributes.GetNamedItem("Crash") != null)
                cp.Crash = Convert.ToBoolean(prog.Attributes.GetNamedItem("Crash").InnerText);

            if (comp.Attributes.GetNamedItem("Multiplier") != null)
                cp.Multiplier = Convert.ToInt16(comp.Attributes.GetNamedItem("Multiplier").InnerText);
            else
                cp.Multiplier = 4;

            if (prog.Attributes.GetNamedItem("Numerator") != null)
                cp.Numerator = Convert.ToByte(prog.Attributes.GetNamedItem("Numerator").InnerText);
            else
                cp.Numerator = composition.Numerator;

            return cp;
        }
    }
}
